package cn.ffcs.uec.view.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffEncEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecEnc;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uec.view.action.bean.UecArticleEditBean;
import cn.ffcs.uec.view.action.bean.UecImportantWorkEditBean;
import cn.ffcs.uec.view.model.UecArticle;
import cn.ffcs.uec.view.model.UecImportantWork;
import cn.ffcs.uec.view.model.UecInnovation;
import cn.ffcs.uec.view.model.UecStaffAll;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
/**
 * @desc 论文专著
 * @author BMP
 * @time 2019-09-05
 */
@Controller
@Scope("prototype")
public class UecArticleEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecArticleEditBean bean = new UecArticleEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	
	
	private UecStaffAll uecStaffAll;
	/**
	 * 人员信息
	 */
	private StaffAndOutStaff uecOutStaffInfo;
	
	private UecArticle uecArticle;


	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecArticleEditWindow() throws ParseException {
		this.bindBean();
	}


	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		uecStaffAll = (UecStaffAll) arg.get("uecStaffAll");
		if ("add".equals(opType)) {
			this.bean.getUecArticleEditWindow().setTitle("新增论文专著情况");
			bean.getUecArticleEditDiv().setHeight("70%");
		}
		/*if ("view".equals(opType)) {
			this.bean.getUecArticleEditWindow().setTitle("查看奖励信息");
			uecArticle = (UecArticle) arg.get("uecArticle");
			if (null != uecArticle) {
				//PubUtil.fillBeanFromPo(uecEnc, bean);
				bean.getButtonDiv().setVisible(false);
				PubUtil.readOnlyComponent(bean.getUecArticleEditDiv(), true);
			}
		}*/
		if ("edit".equals(opType)) {
			this.bean.getUecArticleEditWindow().setTitle("编辑论文专著");
			bean.getUecArticleEditDiv().setHeight("80%");
			uecArticle = (UecArticle) arg.get("uecArticle");
			if (null != uecArticle) {
				PubUtil.fillBeanFromPo(uecArticle, bean);
			}
		}
		
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 获取登录用户
		//String currentName = PlatformUtil.getCurrentUser().getScreenName();
		if ("add".equals(opType)) {
			uecArticle =new UecArticle();
			//PubUtil.fillPoFromBean(bean, uecImportant);
			uecArticle.setPkPsndoc(uecStaffAll.getPkpsndoc());
			uecArticle.setArticleName(bean.getArticleName().getValue());
			if(!"无".equals(bean.getArticleName().getValue().trim())) {
				uecArticle.setPublicationDate(bean.getPublicationDate().getValue());
				uecArticle.setPublicationName(bean.getPublicationName().getValue());
				uecArticle.setWhichauthor(bean.getWhichauthor().getValue());
			}
			uecArticle.setStatuscd("1000");
			// 创建时间
			uecArticle.setCreatetime(new Date());//转换结果为yyyy-mm-dd 00:00:00
			uecArticle.setEfftime(new Date());
			uecArticle.addOnly();
			Events.postEvent("onOK", this.self, uecArticle);
			this.onCancel();
			Messagebox.show("数据新增成功！");
		}
		if ("edit".equals(opType)) {
			//PubUtil.fillPoFromBean(bean, uecImportant);
			//uecArticle.setPkPsndoc(uecStaffAll.getPkpsndoc());
			uecArticle.setArticleName(bean.getArticleName().getValue());
			if(!"无".equals(bean.getArticleName().getValue().trim())) {
				uecArticle.setPublicationDate(bean.getPublicationDate().getValue());
				uecArticle.setPublicationName(bean.getPublicationName().getValue());
				uecArticle.setWhichauthor(bean.getWhichauthor().getValue());
			}
			uecArticle.setModifyname(uecStaffAll.getPkpsndoc());
			// 修改时间
			uecArticle.setStatuscd("1000");
			uecArticle.setUpdatetime(new Date());
			uecArticle.updateOnly();
			Events.postEvent("onOK", this.self, uecArticle);
			this.onCancel();
			Messagebox.show("更新成功！");
		}

	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecArticleEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getArticleName().getValue().trim())) {
			return "请填写论文名称";
		} 
		if(!"无".equals(bean.getArticleName().getValue().trim())) {
	        if (StrUtil.isNullOrEmpty(bean.getPublicationDate().getValue())) {
				return "填写发表日期";
			} else if (StrUtil.isNullOrEmpty(bean.getPublicationName().getValue())) {
				return "填写刊物名称";
			}  else if (StrUtil.isNullOrEmpty(bean.getWhichauthor().getValue())) {
				return "填写第几作者";
			} 
			//字数限制
			if(getLength(bean.getArticleName().getValue().trim())>80) {
				return "论文名称超出字数限制";
			}
			if(getLength(bean.getPublicationName().getValue().trim())>80) {
				return "刊物名称超出字数限制";
			}
		}
		return null;
	}
	public static int getLength(String str) {
		int templ=0;
		templ=str.length();
		return templ;
	}

}
